//
//  OrderDescriptionView.swift
//  ERecycle-Shop
//
//  Created by abb on 16/8/16.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit
import SVProgressHUD

class OrderDescriptionView: UIView {
    
    var order:Order? {
        didSet {
            titleLabel.text = order!.name
            distanceLabel.text = "\(order!.distance!)米"
            
        }
    }
    
    var seeDetailsCallBack:((user:User,order:Order)->())?

    var orderDidReceivedCallBack:(()->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        SVProgressHUD.setDefaultMaskType(.Clear)
        backgroundColor = UIColor.whiteColor()
    }
    
    func receivingOrder() {
        SVProgressHUD.showWithStatus("请稍等")
        OrderDAO.orderReceiving(order!.orderID, DGYunTuId: order!.id) { (success, result) in
            if success {
                SVProgressHUD.showSuccessWithStatus(result)
                self.orderDidReceivedCallBack?()
            } else {
                SVProgressHUD.showErrorWithStatus(result)
            }
        }
    }
    func seeDetails() {
        SVProgressHUD.showWithStatus("请稍等")
        User.loadUserByMerchant(order!.userId) { (model, error) in
            
            if error != nil {
                SVProgressHUD.showErrorWithStatus(error)
                return
            }
            SVProgressHUD.dismiss()
            self.seeDetailsCallBack?(user: model!,order: self.order!)
        }
    }
    
    private func setupUI() {
        addSubview(titleLabel)
        addSubview(distanceLabel)
        addSubview(distanceImageView)
        addSubview(towBtnWithSplitLineView)
        
        titleLabel.snp_makeConstraints { (make) in
            make.left.equalTo(self).offset(20)
            make.top.equalTo(self).offset(10)
            make.right.equalTo(self).offset(-20)
            make.height.equalTo(30)
        }
        distanceLabel.snp_makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp_bottom).offset(10)
            make.left.equalTo(distanceImageView.snp_right).offset(5)
        }
        distanceImageView.snp_makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp_bottom).offset(10)
            make.left.equalTo(titleLabel)
            make.size.equalTo(CGSize(width: 15, height: 15))
        }
        towBtnWithSplitLineView.snp_makeConstraints { (make) in
            make.left.right.equalTo(self)
            make.height.equalTo(50)
            make.top.equalTo(distanceImageView.snp_bottom).offset(10)
        }
        towBtnWithSplitLineView.firstBtn.addTarget(self, action: #selector(receivingOrder), forControlEvents: .TouchUpInside)
        towBtnWithSplitLineView.secondBtn.addTarget(self, action: #selector(seeDetails), forControlEvents: .TouchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private lazy var titleLabel = UILabel.creatLabel(color: UIColor.blackColor(), fontSize: 16)
    
    private lazy var distanceImageView:UIImageView = UIImageView(image: UIImage(named: "arrow"))

    
    private lazy var distanceLabel:UILabel = UILabel.creatLabel(color: UIColor.blackColor(), fontSize: 12)
    
    private lazy var towBtnWithSplitLineView:TowBtnWithSplitLineView = TowBtnWithSplitLineView()
}
